//
//  HNUserViewController.m
//  HappyNews
//
//  Created by baiping on 16/1/11.
//  Copyright © 2016年 BPWBANP. All rights reserved.
//

#import "HNUserViewController.h"
#import "HNUserHeaderView.h"
#import "HNLoginViewController.h"
#import "MBProgressHUD+MJ.h"
#import <UIButton+WebCache.h>
#import "HNPersonalHomePageViewController.h"
#import "HNUserSettingViewController.h"
#import "HNAccountTool.h"
#import "HNUserCell.h"
#define headColor [UIColor colorWithRed:41 / 256.0 green:161 / 256.0 blue:255 / 256.0 alpha:1]

@interface HNUserViewController ()<UITableViewDataSource, UITableViewDelegate, HNLoginViewControllerDelegate, HNPersonalHomePageViewControllerDelegate, HNUserSettingViewControllerDelegate>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) HNUserHeaderView *headerView;

@property (nonatomic, strong) UIView *footerView;

@property (nonatomic, strong) HNLoginViewController *loginVC;

@property (nonatomic, strong) HNPersonalHomePageViewController *personVC;

@property (nonatomic, strong) HNUserSettingViewController*userSettingVC;

@end

@implementation HNUserViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
   // UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 300)];
    
  //  [self.view addSubview:bgView];
    
  //  bgView.backgroundColor = headColor;
    
   // self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"User_Head_Bg"]];
     [self addTableView];
    
      // 添加用户信息
    
    [self addUserInfomatiom];
    
}


- (void)addTableView{
    
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 44, kScreenWidth, kScreenHeight - 44) style:(UITableViewStyleGrouped)];
    
    _tableView.dataSource = self;
    
    _tableView.delegate = self;
    
    // 设置tableView的行高
    _tableView.rowHeight = 45;
    
    _tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    
   // _tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"User_Head_Bg"]];
    
    [self.view addSubview:_tableView];
    
    _headerView = [[HNUserHeaderView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 150)];
    
    _headerView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"User_Head_Bg"]];
    // 设置tableView的头视图
    
    _headerView.iconBtn.layer.cornerRadius = 30;
    
    _headerView.iconBtn.layer.masksToBounds = YES;
    
    [_headerView.iconBtn setBackgroundImage:[UIImage imageNamed:@"User_Icon_Nomal"]  forState:(UIControlStateNormal)];
    
    [_headerView.loninStateBtn setTitle:@"未登陆" forState:(UIControlStateNormal)];
    
    
    [_headerView.massegeBtn setBackgroundImage:[UIImage imageNamed:@"User_Massege_Nomal_"] forState:(UIControlStateNormal)];
    
    
    [_headerView.settingBtn setBackgroundImage:[UIImage imageNamed:@"User_Setting_Nomal"] forState:(UIControlStateNormal)];
    
    // 添加导航控制器自定义视图
    UIView *view =[[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 64)];
    
    view.backgroundColor = [UIColor whiteColor];
    
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(kScreenWidth / 2 - 30, 20, 40, 30)];
    
    titleLabel.text = @"我";
    
    titleLabel.textAlignment = NSTextAlignmentCenter;
    
    [view addSubview:titleLabel];
    
    [self.view addSubview:view];
    
    // 添加导航控制器上面的返回按钮
    UIButton *backBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    
    backBtn.frame = CGRectMake(kScreenWidth - 50, 15, 40, 40);
    
    backBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    
    [backBtn setTitle:@"首页" forState:(UIControlStateNormal)];
    
    [backBtn addTarget:self  action:@selector(backHomeBtnAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
    [backBtn setTitleColor:[UIColor grayColor] forState:(UIControlStateNormal)];
    
    [self.view addSubview:backBtn];
    
    
    // 给设置按钮添加方法
    [_headerView.settingBtn addTarget:self action:@selector(settingBtnAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
    
    self.tableView.tableHeaderView = _headerView;
    
       // 添加头视图下面的几个按钮内容 其中数组默认是0
    _headerView.myVideos.numberLabel.text = [NSString stringWithFormat:@"%d", 0];
    
     _headerView.mySubscription.numberLabel.text = [NSString stringWithFormat:@"%d", 0];
    
     _headerView.myPlayList.numberLabel.text = [NSString stringWithFormat:@"%d", 0];
    
     _headerView.faverite                                                                                                                .numberLabel.text = [NSString stringWithFormat:@"%d", 0];
    
    // 给headView上的backHome按钮添加添加方法
    
    [_headerView.backHomeBtn addTarget:self action:@selector(backHomeBtnAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
    // 实现登陆按钮

    [_headerView.iconBtn addTarget:self action:@selector(loninAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
    [_headerView.loninStateBtn addTarget:self action:@selector(loninAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
    
}

// 实现设置按钮的方法
- (void)settingBtnAction:(UIButton *)sender{
    
    _userSettingVC = [[HNUserSettingViewController alloc]init];
    
    _userSettingVC.delegate = self;
    
    [self.navigationController pushViewController:_userSettingVC animated:YES];
}

// 实现setting界面的代理方法(登录成功之后改变登录状态)
- (void)userSettingViewControllerNeedChangeUserInfamatiom{
    
    [self addUserInfomatiom];
    
}

// 实现登陆按钮的方法
- (HNLoginViewController *)loginVC{
    
    if (!_loginVC) {
        
         _loginVC = [[HNLoginViewController alloc]init];
        
    }
    return _loginVC;
    
}
- (void)loninAction:(UIButton *)sender{
    
   
    BOOL isExistAccount = [[HNAccountTool shareAccountTool]isExistCurrrentAccount];
    
    if (!isExistAccount) {
        
        self.loginVC.view.backgroundColor = [UIColor whiteColor];
        
        _loginVC.delegate = self;
        
        // 登录使用莫泰
        [self presentViewController:self.loginVC animated:YES completion:nil];
        
    }else{
        //    TestViewController *testVC = [[TestViewController alloc]initWithNibName:@"TestViewController" bundle:nil];
        //    [self presentViewController:testVC animated:YES completion:nil];
        
        _personVC = [[HNPersonalHomePageViewController alloc]initWithNibName:@"HNPersonalHomePageViewController" bundle:nil];
        
        // 设置代理
        
        _personVC.delegate = self;
        
        // 登录成功使用push
        [self.navigationController pushViewController:_personVC animated:YES];
    
    }
 
}

#pragma mark ---- 实现personVC代理方法
- (void)personalHomePageViewControllerRemoveLogin{
    
    [self addUserInfomatiom];
    
}

- (void)addUserInfomatiom{
    
    // 设置用户姓名以及用户的微博头像
    
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    
    NSString *userName = [defaults stringForKey:@"name"];
    
    NSString *userIcon = [defaults stringForKey:@"iconURL"];
    
    if (userName ) {
        
        [_headerView.iconBtn sd_setBackgroundImageWithURL:[NSURL URLWithString:userIcon] forState:(UIControlStateNormal)];
        
        [_headerView.loninStateBtn setTitle:userName forState:(UIControlStateNormal)];
        
    }else{
        
        [_headerView.iconBtn setBackgroundImage:[UIImage imageNamed:@"User_Icon_Nomal"] forState:(UIControlStateNormal)];
        
        [_headerView.loninStateBtn setTitle:@"未登陆" forState:(UIControlStateNormal)];
        
    }
    
}
#pragma mark --- 实现loginVC的代理方法
- (void)loginViewControllerDidFinishLogin{
    
    [MBProgressHUD showSuccess:@"登陆成功"];
    
    [self addUserInfomatiom];
    
    [self backHomeBtnAction:nil];
    
    
}

// 实现backHome按钮的点击方法

- (void)backHomeBtnAction:(UIButton *)sender{
    
    if ([_delegate respondsToSelector:@selector(userViewControllerBackHome)] && _delegate) {
        
        [_delegate userViewControllerBackHome];
        
        
    }

}



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 4;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    switch (section) {
        case 0:
            return 1;
            break;
            
        case 1:
            return 2;
            break;

        case 2:
            return 2;
            break;
   
        case 3:
            return 2;
            break;
   
        default:
            break;
    }
    
    
    return 1;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *identifier = @"myCell";
    
    HNUserCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    
    if (!cell) {
        
        cell = [[HNUserCell alloc]initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:identifier];
        
    }
    if (indexPath.section == 0) {
        
       [self cellForImageAndContentWithIcon:@"user_friend" content:@"我的好友" cell:cell];
    }else if (indexPath.section == 1){
        
        if (indexPath.row == 0) {
            
      [self cellForImageAndContentWithIcon:@"user_ noviceTask" content:@"新手任务" cell:cell];
            
        }else{
            
    [self cellForImageAndContentWithIcon:@"user_photo" content:@"我的相册" cell:cell];
            
        }
        
    }else if (indexPath.section == 2){
        
        if (indexPath.row == 0) {
            
    [self cellForImageAndContentWithIcon:@"user_cash" content:@"我的缓存" cell:cell];
            
        }else{
            
    [self cellForImageAndContentWithIcon:@"user_history" content:@"浏览历史" cell:cell];
            
        }
    }else{
        
        if (indexPath.row == 0) {
     
            if (!cell.contentLabel.text) {
    [self cellForImageAndContentWithIcon:@"Menu_Dark" content:@"夜间模式" cell:cell];
                
    }
        
    [cell.unfoldImage removeFromSuperview];
     
        }
    }
    

    return cell;
}
// 传入一个一个图片名和文本
- (void)cellForImageAndContentWithIcon:(NSString *)iconName content:(NSString *)content cell:(HNUserCell *)cell{
    
    cell.icon.image  = [UIImage imageNamed:iconName];
    
    cell.contentLabel.text = content;
  
}

// 实现cell的点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 3 && indexPath.row == 0) {
        
        HNUserCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        
        if ([cell.contentLabel.text isEqualToString:@"夜间模式"]) {
            
            [self changeSkinToNight];
           
            cell.selected = NO;
            
   [self cellForImageAndContentWithIcon:@"Menu_Day" content:@"白天模式" cell:cell];
    
            
        }else{
            
            [self changeSkinToNight];
            
            cell.selected = NO;
            
            [self cellForImageAndContentWithIcon:@"Menu_Dark" content:@"夜间模式" cell:cell];
        }
    }
}

// 切换皮肤
- (void)changeSkinToNight{
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"NIGHT" object:self userInfo:nil];
    
    // 改变 沙盒里面的夜间模式状态
    
    NSUserDefaults *defaluts = [NSUserDefaults standardUserDefaults];
    
    NSString *value = [defaluts objectForKey:@"Night"];
    
    [defaluts setObject:[value isEqualToString:@"YES"] ? @"NO" : @"YES" forKey:@"Night"];
    
    // 同步存储
    [defaluts synchronize];
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
  
}



@end
